import api from '../../api';
import { routerConfig } from '../../utils/outerRouterConfig';
export default {
  namespace: 'dictionaryM',
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        let booleanStr = false;
        routerConfig.forEach((ele) => {
          if (ele.path === pathname) {
            booleanStr = true;
            return false;
          }
        });
        // 迁移资管 进行iv-user设置
        if (booleanStr) {
          let query = location.href.split('?')[1] ? location.href.split('?')[1].split('&')[0] : undefined; //当有url有多个拼接的入参时，取第一个iv-user进行分割 sessionStorage.setItem('iv-user','xxxx')
          if (query) {
            //   sessionStorage.setItem(query.split('=')[0], query.split('=')[1])
            //系统将iv-user去掉 所以这里设置userid
            sessionStorage.setItem('antd-pro-userId', query.split('=')[1]);
          }
        }
      });
    },
  },
  state: {},
  effects: {
    //资管跳转后 获取用户信息,保存租户
    *getUser({ payload, callback }, { call, put }) {
      const response = yield call(api.user.queryCurrent, payload);
      if (response && response.success && response.data) {
        sessionStorage.setItem('maat-antd-pro-tenant', response.data.tenant);
        callback(response);
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
